package com.likai.blog.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.likai.blog.dao.IBlogMapper;
import com.likai.blog.dao.ICommentMapper;
import com.likai.blog.pojo.Blog;
import com.likai.blog.pojo.Comment;
import com.likai.blog.service.ICommentService;
import com.likai.blog.utils.IdUtil;

@Service("commentService")
public class CommentServiceImpl implements ICommentService {
	
	@Autowired
	private ICommentMapper commentMapper ;
	
	@Autowired
	private IBlogMapper blogMapper ;

	@Transactional(readOnly=true)
	public List<Comment> findListByBlogId(String blogId,String state) {
		return commentMapper.selectListByBlogId(blogId,state) ;
	}

	@Transactional
	public int updateInfo(Comment po) {
		String commentId = po.getCommentId() ;
		boolean flag = false ;
		if(commentId == null || "".equals(commentId)) {
			po.setCommentId(IdUtil.uuid());
			if(commentMapper.insertInfo(po) == 1) {
				if(blogMapper.updateByReplyHit(po.getBlogId()) == 1) {
					flag = true ;
				}
			}
		} else {
			if(commentMapper.updateInfo(po) == 1) {
				flag = true ;
			}
		}
		
		if(flag) {
			return 1 ;
		} else {
			return 0 ;
		}
	}

}
